#!/bin/bash
hostname=$1
if [ "$hostname" == "" ]; then
    echo "hostname is null"
    exit
fi

# docker setup
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
update -y
# install tools
yum install -y yum-utils device-mapper-persistent-data lvm2 iproute-tc
yum install -y conntrack ipvsadm ipset jq sysstat curl iptables libseccomp

# install docker
# show docker version
# yum list docker-ce --showduplicates | sort -r
# specify version for install 
# yum install docker-ce-18.09.9 docker-ce-cli-18.09.9 containerd.io
dnf install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io
usermod -aG docker $(whoami)

systemctl daemon-reload 
systemctl enable docker
systemctl start docker

bash -c 'cat <<EOF > /etc/docker/daemon.json
{
    "registry-mirrors": ["https://registry.docker-cn.com"],
    "exec-opts": ["native.cgroupdriver=systemd"],
    "insecure-registries" : ["192.168.99.115"]
}

EOF'

systemctl restart docker

##############################################
# preparatory work
# close selinux
CHECK=$(grep SELINUX= /etc/selinux/config | grep -v "#")
if [ "$CHECK" == "SELINUX=enforcing" ]; then
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    setenforce 0
fi
if [ "$CHECK" == "SELINUX=permissive" ]; then
    sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/selinux/config
    setenforce 0
fi

# set hostname and hosts
hostnamectl set-hostname $hostname
echo "127.0.0.1    $hostname" >> /etc/hosts

# stop and disable firewalld
systemctl stop firewalld
systemctl disable firewalld

# reset iptables
iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT

# swap off
swapoff -ai
#sed -i 's/\/dev\/mapper\/cl-swap/#\/dev\/mapper\/cl-swap/g' /etc/fstab
sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab

# centos 8 has no installed dnsmasq by default
# stop dnsmasq
# systemctl stop dnsmasq
# systemctl disable dnsmasq

# set system parameter
bash -c 'cat <<EOF > /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
vm.swappiness=0
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
EOF'

sysctl -p /etc/sysctl.d/kubernetes.conf

##############################################
# set up kubeadm kubectl kubelet
yum install -y git
bash -c 'cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF'

yum update -y
yum install kubectl kubeadm kubelet -y
# you need to running following command if exist /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
# sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl start kubelet
systemctl enable kubelet

# use following command line to check log after installed
# journalctl -f

# pull necessary image
# list and select version for image
# yum list kubeadm --showduplicates | sort -r
# after pull 
#for IT in coredns:1.7.0 etcd:3.4.9-1 pause:3.2 kube-proxy:v1.19.0 kube-scheduler:v1.19.0 kube-controller-manager:v1.19.0 kube-apiserver:v1.19.0
#do
#    docker image pull "registry.cn-shanghai.aliyuncs.com/yingzhuo/$IT"
#    docker image tag "registry.cn-shanghai.aliyuncs.com/yingzhuo/$IT" "k8s.gcr.io/$IT"
#    docker image rmi "registry.cn-shanghai.aliyuncs.com/yingzhuo/$IT"
#done

# reboot system
reboot
